﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Data.Implements
{
    public class DeTaiCapNhaNuocDao : IDeTaiCapNhaNuocDao
    {
        public List<DeTaiCapNhaNuocView> GetDeTaiCapNhaNuocViews(int chuyenGiaId)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.DeTaiCapNhaNuocViews.Where(x => x.ChuyenGiaId == chuyenGiaId).ToList();
            }
        }

        public List<DeTaiCapNhaNuoc> GetDeTaiCapNhaNuocs(int chuyenGiaId)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.DeTaiCapNhaNuocs.Where(x => x.ChuyenGiaId == chuyenGiaId).ToList();
            }
        }

        public DeTaiCapNhaNuoc Create(DeTaiCapNhaNuoc deTaiCapNhaNuoc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                db.DeTaiCapNhaNuocs.Add(deTaiCapNhaNuoc);
                db.SaveChanges();
            }

            return deTaiCapNhaNuoc;
        }

        public void Update(DeTaiCapNhaNuoc deTaiCapNhaNuoc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                db.DeTaiCapNhaNuocs.Attach(deTaiCapNhaNuoc);
                db.Entry(deTaiCapNhaNuoc).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void Delete(int id)
        {
            if (id <= 0) return;
            using (var db = new NLKHCNDataEntities())
            {
                var deletedItem = new DeTaiCapNhaNuoc {Id = id};
                db.DeTaiCapNhaNuocs.Attach(deletedItem);
                db.Entry(deletedItem).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
    }
}
